package com.ruoyi.web.controller.system;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import com.alibaba.fastjson.JSON;
import com.ruoyi.Order.service.IOrderService;
import com.ruoyi.School.domain.School;
import com.ruoyi.School.service.ISchoolService;
import com.ruoyi.StoreRealMoney.service.IStoreRealmoneyService;
import com.ruoyi.User.service.IUserService;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.APPConstants;
import com.ruoyi.common.utils.DateTools;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.PhoneLoginService;

/**
 * 首页 业务处理
 * 
 * @author ruoyi
 */
@Controller
public class SysIndexController extends BaseController
{
    @Autowired
    private ISysMenuService menuService;
    
    @Autowired
	private IStoreRealmoneyService storeRealmoneyService;
    
    @Autowired
	private IOrderService orderService;
    
    @Autowired
    private ISysRoleService roleService;
    
    @Autowired
	private IUserService userService;
    
    @Autowired
	private PhoneLoginService phoneLoginService;
    
    @Autowired
	private ISchoolService schoolService;
    
    

    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("copyrightYear", Global.getCopyrightYear());
        mmap.put("demoEnabled", Global.isDemoEnabled());
        return "index";
    }

	// 系统介绍
	@GetMapping("/system/main")
	public String main(ModelMap mmap) throws ParseException {
		int schoolId = getSchoolId();
		List<String> orderDate = new ArrayList<String>();
		List<String> orderCount = new ArrayList<String>();
		List<String> orderCountForTUIKUAN = new ArrayList<String>();

		List<String> userAddCount = new ArrayList<String>();
		List<String> riderUserAddCount = new ArrayList<String>();

		List<String> orderCountMonth = new ArrayList<String>();
		List<String> orderCountForTUIKUANMonth = new ArrayList<String>();

		List<String> userAddCountMonth = new ArrayList<String>();
		List<String> riderUserAddCountMonth = new ArrayList<String>();

		for (int i = 7; i > 0; i--) {
			orderDate.add(getStatetime(i));
			String date = getStatetimeForSelect(i);
			int count = orderService.selectCountOnThisDay(date, schoolId);
			int countForTUIKUAN = orderService.selectCountOnThisDayForTUIKUAN(
					date, schoolId);
			orderCountForTUIKUAN.add(String.valueOf(countForTUIKUAN));
			orderCount.add(String.valueOf(count));
			int userCount = userService.selectUserCountForEchart(date);
			userAddCount.add(String.valueOf(userCount));

			int riderUserCount = userService
					.selectRiderUserCountForEchart(date);
			riderUserAddCount.add(String.valueOf(riderUserCount));
		}

		
		/*for (String month : monthList) {
			int countMonth = orderService.selectCountOnThisMonth(month,
					schoolId);
			int countMonthForTUIKUAN = orderService
					.selectCountOnThisMonthForTUIKUAN(month, schoolId);
			orderCountMonth.add(String.valueOf(countMonth));
			orderCountForTUIKUANMonth.add(String.valueOf(countMonthForTUIKUAN));

			int userCountMonth = userService
					.selectUserCountForEchartMonth(month);
			userAddCountMonth.add(String.valueOf(userCountMonth));

			int riderUserCountMonth = userService
					.selectRiderUserCountForEchartMonth(month);
			riderUserAddCountMonth.add(String.valueOf(riderUserCountMonth));
		}*/
		List<Map<String, Object>> result = orderService
				.selectCountForStoreYear(getDeptId(), DateTools.getYearString(), 0);
		if (result.size() > 0 && result.get(0)!=null) {
			Map<String, Object> data = result.get(0);			
			orderCountMonth.add(data.get("JAN").toString());
			orderCountMonth.add(data.get("FEB").toString());
			orderCountMonth.add(data.get("MAR").toString());
			orderCountMonth.add(data.get("APR").toString());
			orderCountMonth.add(data.get("MAY").toString());
			orderCountMonth.add(data.get("JUN").toString());
			orderCountMonth.add(data.get("JULY").toString());
			orderCountMonth.add(data.get("AUG").toString());
			orderCountMonth.add(data.get("SEPT").toString());
			orderCountMonth.add(data.get("OCT").toString());
			orderCountMonth.add(data.get("NOV").toString());
			orderCountMonth.add(data.get("DEC").toString());
		}
		
		List<Map<String, Object>> resultTUIKUAN = orderService
				.selectCountForStoreYear(getDeptId(), DateTools.getYearString(), 1);
		if (resultTUIKUAN.size() > 0 && resultTUIKUAN.get(0)!=null) {
			Map<String, Object> data = resultTUIKUAN.get(0);			
			orderCountForTUIKUANMonth.add(data.get("JAN").toString());
			orderCountForTUIKUANMonth.add(data.get("FEB").toString());
			orderCountForTUIKUANMonth.add(data.get("MAR").toString());
			orderCountForTUIKUANMonth.add(data.get("APR").toString());
			orderCountForTUIKUANMonth.add(data.get("MAY").toString());
			orderCountForTUIKUANMonth.add(data.get("JUN").toString());
			orderCountForTUIKUANMonth.add(data.get("JULY").toString());
			orderCountForTUIKUANMonth.add(data.get("AUG").toString());
			orderCountForTUIKUANMonth.add(data.get("SEPT").toString());
			orderCountForTUIKUANMonth.add(data.get("OCT").toString());
			orderCountForTUIKUANMonth.add(data.get("NOV").toString());
			orderCountForTUIKUANMonth.add(data.get("DEC").toString());
		}
		
		List<Map<String, Object>> resultUser = userService
				.selectUserCountForInformationYear(0, DateTools.getYearString());
		if (resultUser.size() > 0 && resultUser.get(0)!=null) {
			Map<String, Object> data = resultUser.get(0);
			userAddCountMonth.add(data.get("JAN").toString());
			userAddCountMonth.add(data.get("FEB").toString());
			userAddCountMonth.add(data.get("MAR").toString());
			userAddCountMonth.add(data.get("APR").toString());
			userAddCountMonth.add(data.get("MAY").toString());
			userAddCountMonth.add(data.get("JUN").toString());
			userAddCountMonth.add(data.get("JULY").toString());
			userAddCountMonth.add(data.get("AUG").toString());
			userAddCountMonth.add(data.get("SEPT").toString());
			userAddCountMonth.add(data.get("OCT").toString());
			userAddCountMonth.add(data.get("NOV").toString());
			userAddCountMonth.add(data.get("DEC").toString());
		}
		
		List<Map<String, Object>> resultRider = userService
				.selectUserCountForInformationYear(1, DateTools.getYearString());
		if (resultRider.size() > 0 && resultRider.get(0)!=null) {
			Map<String, Object> data = resultRider.get(0);
			riderUserAddCountMonth.add(data.get("JAN").toString());
			riderUserAddCountMonth.add(data.get("FEB").toString());
			riderUserAddCountMonth.add(data.get("MAR").toString());
			riderUserAddCountMonth.add(data.get("APR").toString());
			riderUserAddCountMonth.add(data.get("MAY").toString());
			riderUserAddCountMonth.add(data.get("JUN").toString());
			riderUserAddCountMonth.add(data.get("JULY").toString());
			riderUserAddCountMonth.add(data.get("AUG").toString());
			riderUserAddCountMonth.add(data.get("SEPT").toString());
			riderUserAddCountMonth.add(data.get("OCT").toString());
			riderUserAddCountMonth.add(data.get("NOV").toString());
			riderUserAddCountMonth.add(data.get("DEC").toString());
		}
		
		
		String orderDateJson = JSON.toJSON(orderDate).toString();

		List<String> orderDateMonth = getYearMonth();
		String orderDateMonthJson = JSON.toJSON(orderDateMonth).toString();
		/*
		 * String orderCountJson=JSON.toJSON(orderCount).toString(); String
		 * orderCountForTUIKUANJson
		 * =JSON.toJSON(orderCountForTUIKUAN).toString();
		 */
		// 数字数组不必转成json
		mmap.put("orderDateJson", orderDateJson);// 近一周日期
		mmap.put("orderDateMonthJson", orderDateMonthJson);
		mmap.put("orderCountForTUIKUANJson", orderCountForTUIKUAN);
		mmap.put("orderCountJson", orderCount);
		mmap.put("orderCountMonthJson", orderCountMonth);
		mmap.put("orderCountForTUIKUANMonthJson", orderCountForTUIKUANMonth);
		mmap.put("userAddCountJson", userAddCount);
		mmap.put("riderUserAddCountJson", riderUserAddCount);
		mmap.put("userAddCountMonthJson", userAddCountMonth);
		mmap.put("riderUserAddCountMonthJson", riderUserAddCountMonth);
		SysUser user=ShiroUtils.getSysUser();
		mmap.put("user", user);
		int riderVerifyCount=userService.selectRiderVerifyCount();
		mmap.put("riderVerifyCount", riderVerifyCount);
		

		List<Map<String, Object>> orderList = phoneLoginService
				.CallShellProcedure("R2011008|" + getSchoolId());
		List<Map<String, Object>> moneyList = orderService.selectMoneyEveryday(getDeptId());
		if (moneyList.size() > 0 && moneyList != null) {
			mmap.put("moneyDay", moneyList.get(0).get("DAY"));
			mmap.put("moneyMonth", moneyList.get(0).get("MONTH"));
			mmap.put("moneyYear", moneyList.get(0).get("YEAR"));
		}

		mmap.put("orderDay", orderList.get(0).get("dds"));
		mmap.put("orderMonth", orderList.get(1).get("dds"));
		mmap.put("orderYear", orderList.get(2).get("dds"));

		mmap.put("accountMoney", ShiroUtils.getSysUser().getAccountMoney());
		mmap.put("orderCountAll", orderService.selectCountAll(getSchoolId()));

		String storeName = ShiroUtils.getSysUser().getUserName();

		double got = storeRealmoneyService.selectRealMoneyGot(storeName);
		mmap.put("accountMoneyGot", got);
		
		int verifyOrderCount=orderService.selectVerifyOrderCount(getDeptId());
		mmap.put("verifyOrderCount", verifyOrderCount);

		String userName = ShiroUtils.getSysUser().getUserName();
		if (userName != null) {
			if (APPConstants.XITONGGUANLIYUAN.equals(userName)) {
				return "main_v1";
			} else {
				return "main_v1";
			}
		}
		return "main_v1";
	}

	public String getStatetime(int i) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, -i);
		Date monday = c.getTime();
		String preMonday = sdf.format(monday);
		return preMonday;
	}

	public String getStatetimeForSelect(int i) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, -i);
		Date monday = c.getTime();
		String preMonday = sdf.format(monday);
		return preMonday;
	}

	// 获取当前年份的月份数组
	public List<String> getYearMonth() throws ParseException {
		List<String> months = new ArrayList<String>();
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		for (int i = 1; i < 13; i++) {
			String StringI = "";
			if (i < 10) {
				StringI = "0" + String.valueOf(i);
			} else {
				StringI = String.valueOf(i);
			}
			String example = String.valueOf(year) + "-" + StringI;
			months.add(example);
		}
		return months;
	}

	public int getSchoolId() {
		Integer schoolId = ShiroUtils.getSysUser().getDept().getSchoolId();
		if(schoolId!=null){
			return schoolId;
		} else {
			return 0;
		}		
	}
	
	//schoolId改拿部门Id
			public String getDeptId() {
				SysDept dept=ShiroUtils.getSysUser().getDept();
				if(dept!=null){
					String id=dept.getDeptId().toString();
					return id;						
				} else {
					return StringUtils.EMPTY;
				}
			}
	
	
}
